from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from admincharge.models import SellPet,ShopGoods
from PetNetadmin.serializers import SellPetSerializer,ShopGoodsSerializer
from utils.pagination import PetMessagePagination
# 主页的售宠数据
class HomePageMessage(APIView):
    def get(self,request):
        keyword = request.GET.get('keyword')
        # print(keyword)
        if keyword !='全部':
            All_message = SellPet.objects.filter(catagory__contains=keyword)
        else:
            All_message = SellPet.objects.all()
        # 分页处理
        paginator = PetMessagePagination()
        result_page = paginator.paginate_queryset(All_message, request)
        serializer = SellPetSerializer(instance=result_page, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return paginator.get_paginated_response(result)
# 宠物detail页面
@api_view(['GET'])
def getAllPetList(request,id):
    # print(id)
    sellMsg = SellPet.objects.get(pk=id)
    serializer = SellPetSerializer(instance=sellMsg)
    result = {
        'data': serializer.data,
        'code': 2000,
        'msg': '获取成功'
    }
    return Response(result,status=status.HTTP_200_OK)
# 粮食detail页面
@api_view(['GET'])
def getAllGoodsList(request,id):
    # print(id)
    sellMsg = ShopGoods.objects.get(pk=id)
    serializer = ShopGoodsSerializer(instance=sellMsg)
    result = {
        'data': serializer.data,
        'code': 2000,
        'msg': '获取成功'
    }
    return Response(result,status=status.HTTP_200_OK)

# 主页的粮食商品展示
class ShopGoodsHomeMessage(APIView):
    def get(self, request):
        keyword = request.GET.get('keyword')
        print(keyword)
        if keyword != '全部':
            All_message = ShopGoods.objects.filter(shopgoods_catagory__contains=keyword)
        else:
            All_message = ShopGoods.objects.all()
        # 分页处理
        paginator = PetMessagePagination()
        result_page = paginator.paginate_queryset(All_message, request)
        serializer = ShopGoodsSerializer(instance=result_page, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return paginator.get_paginated_response(result)


